package com.example.complexrecyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    int colors[] = {android.R.color.holo_red_dark,
            android.R.color.holo_blue_dark,
            android.R.color.holo_orange_dark};
    private RecyclerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = findViewById(R.id.recyclerView);
//        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
//                LinearLayoutManager.VERTICAL, false));

        final GridLayoutManager gridLayoutManager =
                new GridLayoutManager(this, 2);//2列=分母
        //解决布局被覆盖的问题
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

            @Override
            public int getSpanSize(int position) { //返回的int值=分子，占一行的几分之几
                int type = mRecyclerView.getAdapter().getItemViewType(position);
                if (type == RecyclerAdapter.TYPE_THREE) { //第三类占一行
                    return gridLayoutManager.getSpanCount(); //就是上面的设置2 spandCount
                } else {
                    return 1; //其他两类占1/2，一行分2列
                }

            }
        });
        mRecyclerView.setLayoutManager(gridLayoutManager);



        mAdapter = new RecyclerAdapter(this);
        mRecyclerView.setAdapter(mAdapter);


        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                GridLayoutManager.LayoutParams layoutParams =
                        (GridLayoutManager.LayoutParams) view.getLayoutParams();
                int spanSize = layoutParams.getSpanSize();//上面getSpanSize方法的返回值，分子
                int spanIndex = layoutParams.getSpanIndex();
                outRect.top = 20; //距顶部的距离

                //gridLayoutManager.getSpanCount() 返回值为2，一行分成2列，上面自行设置的参数
                int v = gridLayoutManager.getSpanCount();

                if (spanSize != v) { //分子与分母不一样的，也就是被grid分开的
                    if (spanIndex == 1) {//右侧的块
                        outRect.left = 10;
                    } else {//左侧的块
                        outRect.right = 10;
                    }
                }
            }
        });
        initData();
    }

    private void initData() {
        /*List<DataModel> list = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            //int type = (int) ((Math.random()*3) + 1);
            int type;
            if (i < 5 || (i > 15 && i < 20)) {
                type = 1;
            } else if (i < 10 || i > 26) {
                type = 2;
            } else {
                type = 3;
            }
            DataModel data = new DataModel();
            data.avatarColor = colors[type - 1];
            data.type = type;
            data.name = "name :" + type;
            data.content = "content :" + i;
            data.contentColor = colors[(type + 1) % 3];
            list.add(data);
        }*/

        List<DataModelOne> list1 = new ArrayList<DataModelOne>();
        for (int i = 0; i < 10; i++) {
            DataModelOne data = new DataModelOne();
            data.avatarColor = colors[0];
            data.name = "name :" + 1;
            list1.add(data);
        }

        List<DataModelTwo> list2 = new ArrayList<DataModelTwo>();
        for (int i = 0; i < 10; i++) {
            DataModelTwo data = new DataModelTwo();
            data.avatarColor = colors[1];
            data.name = "name :" + 2;
            data.content = "content";
            list2.add(data);
        }

        List<DataModelThree> list3 = new ArrayList<DataModelThree>();
        for (int i = 0; i < 10; i++) {
            DataModelThree data = new DataModelThree();
            data.avatarColor = colors[2];
            data.name = "name :" + 3;
            data.content = "content";
            data.contentColor = colors[2];
            list3.add(data);
        }

        mAdapter.addList(list1, list2, list3);
        mAdapter.notifyDataSetChanged();
    }

}